//
//  YSCardMoreCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/16.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSCardMoreCtrl.h"
#import "YSHomeCardImageCell.h"
#import "YSHomeDataTool.h"
#import "YSCardMoreRes.h"

@interface YSCardMoreCtrl () <UITableViewDataSource,UITableViewDelegate>
@property (assign, nonatomic) NSInteger cardId;
@property (copy, nonatomic) NSString *cardName;
@property (strong, nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *allFrames;
@property (strong,nonatomic) NSMutableArray *itemArray;
@property (assign, nonatomic) NSInteger pageIndex;
@end

@implementation YSCardMoreCtrl

- (instancetype)initWithCardId:(NSInteger)cardId cardName:(NSString *)cardName{
    self = [super init];
    if (self) {
        _cardId = cardId;
        _cardName = cardName;
    }
    return self;
}

-(NSMutableArray *)allFrames{
    if(!_allFrames){
        _allFrames = [NSMutableArray array];
    }
    return _allFrames;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:self.cardName];
    [self initTableView];
    
    [self requestData];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setBackgroundColor:PUBLIC_CARD_BG];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.header = header;
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
}

-(void)requestData{
    [YSHomeDataTool getCardMoreWithCardId:self.cardId pageIndex:self.pageIndex success:^(YSCardMoreRes *result) {
        [UIUtil refreshComplete:self.tableView];
        if(result.isSucc){
            if(self.pageIndex == 0){
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.advertList];
            [self itemArrayWithFrames];
        }else{
            ShowEnterTips(result.data);
        }

    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
}

-(void)itemArrayWithFrames{
    [_allFrames removeAllObjects];
    for (YSAdvert *advert in _itemArray) {
        YSCardImageItemFrame *frame = [[YSCardImageItemFrame alloc] init];
        // 传递模型数据，计算所有子控件的frame
        frame.advert = advert;
        [_allFrames addObject:frame];
    }
    
    [_tableView reloadData];
}
     
-(void)headerRefresh{
    self.pageIndex = 0;
    [self requestData];
}

-(void)footerRefresh{
    self.pageIndex ++;
    [self requestData];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allFrames.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSHomeCardImageCell *cell =  [YSHomeCardImageCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.cardImageFrame = self.allFrames[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_allFrames[indexPath.row] cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSCardImageItemFrame *frame = self.allFrames[indexPath.row];
    [Utils dealUrlWithController:self url:frame.advert.advertUrl fromWebView:NO];
}

@end
